package com.example.chapter3.service.iml;
import com.example.chapter3.Dao.UserDao;
import com.example.chapter3.entity.User;
import com.example.chapter3.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

@Service
public class UserServiceIml implements UserService {
    @Autowired
    private UserDao userDao;

    @Override
    public int register(User user) {
        int accountNumber = userDao.findUserByName(user.getUsername());
        if (accountNumber!=0){
            //用户名在数据库中存在，注册失败,抛出异常
            throw new RuntimeException("用户已存在");
        }
        //如果数据库中不存在这个用户名，就添加到数据库中去
        return userDao.creatUser(user);
    }
    private final JdbcTemplate jdbcTemplate;
    public UserServiceIml(JdbcTemplate jdbcTemplate){
        this.jdbcTemplate=jdbcTemplate;
    }
    @Override
    public int addUser(User user){
        String sql="INSERT INTO user(username,password,sex)VALUES(?,?,?)";
         return jdbcTemplate.update(sql,user.getUsername(),user.getPassword(),user.getSex());

    }


    @Override
    public int updateUser(User user) {
        return 0;
    }

    @Override
    public int deleteUser(int id) {
        return 0;
    }

    @Override
    public User findUser(int id) {
        return null;
    }
}
